<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace app\shop\api;
use app\common\api\BaseApi;
use app\shop\model\Category;


class CategoryApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "shop/CategoryApi/add";
    /**
     * 保存
     */
    const SAVE = "shop/CategoryApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "shop/CategoryApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "shop/CategoryApi/delete";

    /**
     * 查询
     */
    const QUERY = "shop/CategoryApi/query";
    /**
     * 不分页，查询
     */
    const QUERY_NO_PAGING = "shop/CategoryApi/queryNoPaging";
    /**
     * 查询一条数据
     */
    const GET_INFO = "shop/CategoryApi/getInfo";
    const GET_ALL  = "shop/CategoryApi/getCates2";

	protected function _init(){
		$this->model = new Category();
	}

    public function getCates2($order = "display_order asc",$field = "id,name,parent,level,icon"){
        $cat = [];
        $tempcat = [];
        $arr = [];
        // 1.分步查询
        // $result = $this->queryNoPaging(['parent'=>0,'level'=>1],$order,$field);
        // if(!$result['status']) return $this->apiReturnErr($result['info']);
        // foreach ($result['info'] as $v) {
        //     $cid = $v['id'];
        //     $rr  = ['id'=>$v['id'],'name'=>$v['name'],'child'=>[]];
        //     $result = $this->queryNoPaging(['parent'=>$cid,'level'=>2],$order,$field);
        //     if(!$result['status']) return $this->apiReturnErr($result['info']);
        //     $tmp = [];
        //     foreach ($result['info'] as $vv) {
        //         $tmp[] = ['id'=>$vv['id'],'name'=>$vv['name']];
        //     }
        //     $rr['child'] = $tmp;
        //     unset($tmp);
        //     $arr[] = $rr;
        //     unset($rr);
        // }
        // 2.统一组装
        $r = $this->queryNoPaging(null,$order,$field);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        //分类重组
        foreach ($r['info'] as $v) {
            $cat[$v['id']] = $v;
            $tempcat[$v['id']] = $v;
        }
        //添加一级类目
        foreach ($tempcat as $k=>$v) {
            $level  = intval($v['level']);
            if(1 === $level && 0 === intval($v['parent'])){
                $arr[$v['id']] = ['id'=>$v['id'],'name'=>$v['name'],'icon'=>$v['icon'],'child'=>[]];
                unset($tempcat[$k]);
            }
        }
        //添加二级类目
        foreach ($tempcat as $k=>$v) {
            $level  = intval($v['level']);
            if(2 === $level && isset($arr[$v['parent']])){
                $arr[$v['parent']]['child'][$v['id']] = ['id'=>$v['id'],'name'=>$v['name'],'icon'=>$v['icon'],'child'=>[]];
                unset($tempcat[$k]);
            }
        }
        //添加三级类目
        foreach ($tempcat as $k=>$v) {
            $level   = intval($v['level']);
            $parent  = $v['parent'];
            if(!isset($cat[$parent])) continue;
            $grantpa = $cat[$parent]['parent'];
            if(3 === $level && isset($arr[$grantpa])){
                $arr[$grantpa]['child'][$parent]['child'][] = ['id'=>$v['id'],'name'=>$v['name'],'icon'=>$v['icon']];
                unset($tempcat[$k]);
            }
        }
        unset($cat);
        unset($tempcat);
        $arr = array_values($arr);
        foreach ($arr as &$v) {
            $v['child'] = array_values($v['child']);
        }
        //3.return
        return $this->apiReturnSuc($arr);
    }
}